//
//  SoundController.m
//  AloneInTheLight
//
//  Created by George Karalis on 10/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SoundController.h"

#define SPLASH @"Sound/SplashTest1.caf"
#define WIND @"Sound/WindLoopReal1.caf"
#define SING @"Sound/Sin220.caf"
#define SHARD1 @"Sound/ShortShardSound1.caf"

#define RAMP_AMT 350
#define FADE_STEPS 100


@implementation SoundController

- (id)init
{
    self = [super init];
    if (self) {
        device = [[ALDevice deviceWithDeviceSpecifier:nil] retain];
        context = [[ALContext contextOnDevice:device attributes:nil] retain];
        [OpenALManager sharedInstance].currentContext = context;
        
        [OALAudioSession sharedInstance].handleInterruptions = YES;
        // Don't allow iPod music to keep playing
        [OALAudioSession sharedInstance].allowIpod = NO; 
        // Mute if silent switch is on
        [OALAudioSession sharedInstance].honorSilentSwitch = YES;
        
        // Take all 32 audio sources
        channel = [[ALChannelSource channelWithSources:10] retain];
        
        /** Preload buffers here **/
        splashBuffer = [[[OpenALManager sharedInstance] bufferFromFile: SPLASH] retain];
        singBuffer = [[[OpenALManager sharedInstance] bufferFromFile:SING] retain];
        shard1Buffer = [[[OpenALManager sharedInstance] bufferFromFile: SHARD1] retain];
        
        // Background sound loop
        windTrack = [[OALAudioTrack track] retain];
        
        //Set up sing source
        singSource = [[ALSource source] retain];
        
        currSingPitch = -1.0;
        nextSingPitch = -1.0;
        ramp = 0;
        
        fadeStep = 0;
        
    }
    return self;
}

- (void) dealloc
{
    // Umm don't think this is the best place to do this but
    // whatever for now
    [windTrack release];
    [channel release];
    [context release];
    [device release];
    
    [splashBuffer release];
    [singSource release];
    
    [super dealloc];
}

- (void) startGameStartSounds {
    // Start the background sound loops
    [windTrack playFile: WIND loops:-1];
}

- (void) playSplash {
    [channel play: splashBuffer];
}

- (void) pauseAllSounds {
    windTrack.paused = YES;
    channel.paused = YES;
}

- (void) resumeAllSounds {
    channel.paused = NO;
    windTrack.paused = NO;
}

- (void) stopAllSounds {
    // For when the game is quit
    [channel stop];
    [windTrack stop];
}

- (void) startSinging {
    
    [singSource play:singBuffer gain:0.0f pitch:1 pan:0 loop:YES];
    [singSource fadeTo:1.0f duration:1 target:nil selector:nil];
    
}

- (void) onFadeComplete:(id) sender {
    singSource.pitch = 1;
    [singSource stop];
}

- (void) setSingPitch: (long double) frequency {
    nextSingPitch = frequency;
    ramp = (nextSingPitch - currSingPitch) / RAMP_AMT;
}

- (void) updateSingPitch {
    // Ramp pitch change
    if (currSingPitch - nextSingPitch > .1 || currSingPitch - nextSingPitch < -.1) {
        singSource.pitch += ramp;
        currSingPitch = singSource.pitch;
        //NSLog(@"currSingPitch = %Lf, nextSingPitch = %Lf", currSingPitch, nextSingPitch);
    }
}

- (void) stopSinging {
    [singSource fadeTo: 0 duration: 1 target: nil selector:@selector(onFadeComplete:)];
}

- (void) playActivateShard: (NSInteger) sound {
    if (sound == 1) [channel play:shard1Buffer];
}

@end
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
